var names=$.cookie("username")?JSON.parse($.cookie("username")):[];
if($.cookie("username")){

    huanying=`<div class="dc fl">欢迎${names}</div>`;
    $('.fr .dc').html(huanying);
    gouwuchexuanran();
    
}else{
    alert('请先登陆');
    location.href=('../html/小米有品登陆.html');
}
let leibie='';
$.get('../html/getGoodsType.php',function(data){
    var fenleichuangcao=JSON.parse(data);

    fenleichuangcao.forEach(item=>{
        leibie+=`<div class="chuangzao" onclick='location.href="./商品栏界面.html?typesid=${item.typeId}"'>${item.goodsType}</div>`
    })
    $('.fenleishangpin').html(leibie);
    
    console.log(data);
    console.log(leibie);
})

function gouwuchexuanran(){
    let str='';
    $.get('getShoppingCart.php',{vipName:names},function(data){
        var res=JSON.parse(data); 
        var idlist=[];
        var zongshu=0;
        var zongjia=0;
        window.idlist=idlist;
        res.forEach(function(item){
            idlist.push(item.goodsId)
            zongshu =zongshu +Number(item.goodsCount);
            zongjia=zongjia +item.goodsPrice*item.goodsCount;
            str+=`<div class="dange">
            <div class="checkbox">
                <input type="checkbox">
            </div>
            <div class="imgs">
                <img src="${item.goodsImg}" alt="">
            </div>
            <div class="name">
                <div class="wrap">
                    <p>${item.goodsName}</p>
                </div>
            </div>
            <div class="price">
                <span>￥ ${item.goodsPrice}</span>
            </div>
            <div class="num">
                <div class="canshu">
                    <div class="num-add">
                        <div class="min-plus jian">-</div>
                        <span>${item.goodsCount}</span>
                        <div class="min-plus jia" >+</div>
                    </div>
                </div>
            </div>
            <div class="jine">
                <span>￥ ${item.goodsPrice*item.goodsCount}</span>
            </div>
            <div class="del">x</div>
        </div>`;
        })
        ;
    $('.shoppinglist').html(str);
    window.zongshu=zongshu;
    $('.zongjianshu').html(`已选${zongshu}件`);
    $('.heji').html(`<span>合计￥${zongjia}元</span>`)
    })
}
//点击减号减少数量
$('.shoppinglist').on('click','.jian',function(){
    console.log("我被点了");
    var index=$('.shoppinglist .jian').index(this);//获取下标
    var count=$(this).parent().children().eq(1).text();
    count--;
    if(count<1){
        count=1
    }else{
        $.get("../html/updateGoodsCount.php",{vipName:names,goodsId:idlist[index],goodsCount:count})
        gouwuchexuanran();
    }

})
//点击加号增加数量
$('.shoppinglist').on('click','.jia',function(){
    console.log("我被点了");
    var index=$('.shoppinglist .jia').index(this);//获取下标
    var count=$(this).parent().children().eq(1).text();
    count++;
  
        $.get("../html/updateGoodsCount.php",{vipName:names,goodsId:idlist[index],goodsCount:count})
        gouwuchexuanran();
})
//点击删除
$('.shoppinglist ').on('click','.del',function(){
    console.log('我被地单；额');
    var index=$('.shoppinglist .del').index(this);//获取删除按钮下标
    $.get('../html/deleteGoods.php',{vipName:names,goodsId:idlist[index]})
    gouwuchexuanran();
})
